package org.dragonnova.meetingapp.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.widget.TextView;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.app.adapter.ChatListAdapter;
import org.dragonnova.meetingapp.model.bean.MeetingInfo;
import org.dragonnova.meetingapp.model.bean.VoiceInfo;
import org.dragonnova.meetingapp.utils.ContentResolverUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {

    private ListView mListView;
    private ChatListAdapter mAdapter;

    private ArrayList<VoiceInfo> chatList = new ArrayList<>();//存放聊天记录的list
    private MeetingInfo mMeetingInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        Intent intent = getIntent();
        if (intent != null) {
            Serializable serializable = intent.getSerializableExtra("meetingInfo");
            if (serializable != null) {
                mMeetingInfo = (MeetingInfo) serializable;
            } else {
                throw new RuntimeException("MeetingGuideActivity received an action that not defined");
            }
        }

        initView();
        initData();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            TextView history_tb_tv_title = (TextView) toolbar.findViewById(R.id.history_tb_tv_title);
            if (mMeetingInfo != null) {
                history_tb_tv_title.setText(mMeetingInfo.getName());
            }
        }

        mListView = (ListView) this.findViewById(R.id.history_lv);
        //初始化聊天记录
        mAdapter = new ChatListAdapter(HistoryActivity.this, chatList);
        mListView.setAdapter(mAdapter);
    }

    public void initData() {
        if (mMeetingInfo != null) {
            showHistory();
        }
    }

    /**
     * 显示聊天记录
     */
    private void showHistory() {
        chatList.clear();
        ContentResolverUtil resolverUtil = new ContentResolverUtil(this);
        List<VoiceInfo> history = resolverUtil.findHistoryById(String.valueOf(mMeetingInfo.getSerial()));
        chatList.addAll(history);
        mAdapter.notifyDataSetChanged();
        mListView.setSelection(mAdapter.getCount() - 1);
    }
}
